#!/usr/bin/perl -w
use strict;
=cut
my $parse_tmp_file_path_string = "bwa_sam_tmp";
my $parse_sam_exe_string = "bwa_sam_sam_exe";
my $parse_fa_file_string = "bwa_sam_fa_file";
my $parse_hadoop_exe_string = "bwa_sam_hadoop";
my $parse_hadoop_bam_int_url_string = "bwa_sam_bam_int";
=cut

my $sam_exe="/bluearc/data/schatz/software/bin/samtools";
my $hadoop_exe="/bluearc/data/schatz/software/bin/hadoop";
my $hadoop_bam_int_url="/indel/";

my $magic_dir="/bluearc/data/schatz/hadoop/";
my $fa_file_path="/bluearc/data/schatz/hadoop/NC_000913.fna";
my $tmp_file_path="/tmp/aaithal";
#system ("rm -rf $tmp_file_path");
system("mkdir $tmp_file_path");

my $machine_name = `hostname`;
chomp ($machine_name);
my $hdfspath = $magic_dir.$machine_name."/";
#system("rm -rf $hdfspath");
system("mkdir $hdfspath");


my $path = $hdfspath;
#my $path = "/tmp/aaithal/";
my $bam_name = "out.bam";
my $sam_name = "out.sam";
my $samtools = $sam_exe;
my $sorted_bam_name ="out.prefix";
my $FA_path = $fa_file_path;
my $super_bam_name = "super.bam";
my $pileup_name = "pileup.txt";
my $hadoop = $hadoop_exe;
my $bam_h = "bam/";

while(<>){
	my $line = $_;
	$line =~ s/\[NEWLINE\]/\\\n/g;	
	$line =~ s/\\//g;	
	my $fh;
	my $outsampath;
	my $counter = rand(100000);
=cut
	while(1){
		$outsampath = $path.$counter.$sam_name;
		if(-e $outsampath){
			$counter++;
		}else{
			last;
		}
	}
=cut
	$outsampath = $path.$counter.$sam_name;
	open $fh,">".$outsampath or die "could not open at $!";
	print $fh $line;
	my $llen = length($line);
	print STDERR "len_of_file: $llen\n";
	close $fh;

	my $outbampath = $path.$counter.$bam_name;
	print STDERR "[adbg] outbampath: $outbampath\n";
	print STDERR "[adbg] outsampath: $outsampath\n";
	system("$samtools view -bSu $outsampath > $outbampath") == 0 or die "could not execute at $!";

	my $sortedbampath = $path.$counter.$sorted_bam_name;
	my $tempsortedbampath = $path.".".$counter.$sorted_bam_name;

	system("$samtools sort $outbampath $sortedbampath") == 0 or die "could not execute at $!";

	$sortedbampath .= ".bam";
	system("$samtools index $sortedbampath") == 0 or die "could not execute at $!";

	my $pileup_fname = $counter.".pileup";
	system("$samtools mpileup -f $fa_file_path $sortedbampath > $pileup_fname") == 0 or die "could not execute at $!";

	open pileup_fh,$pileup_fname;
	while(<pileup_fh>){
		print $_;
	}
	close pileup_fh;

=cut
	my $lsop = `ls -l $path`;
	print STDERR "lsop: $lsop";
####################################################################################
#	print STDERR "hadoopurl: hadoop fs -put $sortedbampath $hadoop_bam_int_url\n";
	#system("hadoop fs -put $sortedbampath $hadoop_bam_int_url$counter$sorted_bam_name") == 0 or die "Could not execute at $!";
#	system("hadoop fs -put $sortedbampath $hadoop_bam_int_url") == 0 or die "Could not execute at $!";
#	
#	my $dummy;
	open fh_local, "<", $sortedbampath or die "could not open at $!";
#	open(fh_local,$sortedbampath);
#	while(<fh_local>){
#		chomp;
#		$dummy .= $_;
#	}
#	$dummy = <fh_local>;	
	my @sortedbamcont = <fh_local>;
	$dummy = join('',@sortedbamcont);
	#$dummy =~ s/\t/TAB/g;
	#$dummy =~ s/\s/SPA/g;
	#$dummy =~ s/\n/NEW/g;
	$dummy .="\n";
	print STDERR "key: $sortedbampath\n";
	print $sortedbampath."\t".$dummy;
	$dummy = 0;
	close fh_local;
##[sanjeev] append data along with some ID,red.pl should read these 2 components and use merge over all collected copies,locally

###############################################################################

#	print STDERR "[adbg] wrap_sam: hadoop_bam_int_url$counter$sorted_bam_name [adbg]\n";
#	print $hadoop_bam_int_url.$counter.$sorted_bam_name." ";
#	system("mv $sortedbampath $tempsortedbampath") == 0 or die "Could not execute at $!";
#	$counter++;
#	print STDERR "[adbg] counter: $counter";
=cut
}

#system ("rm -rf $tmp_file_path*");
#system ("rm -rf $tmp_file_path");
#system("rm -rf $hdfspath");


